package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.DeviceDao;
import com.huilian.iotbox.admin.wap.dao.DeviceTypeDao;
import com.huilian.iotbox.admin.wap.service.DeviceTypeService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.po.Device;
import com.huilian.iotbox.data.po.DeviceType;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.data.vo.LedgerMerchantVo;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DeviceTypeServiceImpl implements DeviceTypeService {

    @Resource
    private DeviceTypeDao deviceTypeDao;

    @Resource
    private DeviceDao deviceDao;

    @Override
    public List<DeviceType> findAllDeviceType() {
        return deviceTypeDao.findAllDeviceType();
    }

    @Override
    public List<DeviceType> findCurrDeviceType(UserInfo userInfo) {
        return deviceTypeDao.findCurrDeviceType(userInfo);
    }

    @Override
    public ServerResponse findDeviceBydeviceIdGrouping(DeviceDto deviceDto) {
        if(StringUtils.isEmpty(deviceDto.getMerchantStoreId())){
            return ServerResponse.createByErrorMessage("场地Id不存在");
        }
        if(StringUtils.isEmpty(deviceDto.getIsInsertCoinType())){
            return ServerResponse.createByErrorMessage("查询分支不存在");
        }
        List<DeviceVo> deviceVoList = deviceDao.findDeviceBydeviceIdGrouping(deviceDto);
        List<DeviceVo> arrayList = deviceVoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(DeviceVo::getDeviceTypeId))), ArrayList::new));
        List list = new ArrayList<>();
        log.info("----->数据"+arrayList.size());
        for (DeviceVo t : arrayList){
            DeviceVo deviceVo = new DeviceVo();
            deviceVo.setDeviceTypeId(t.getId());
            deviceVo.setDeviceTypeName(t.getDeviceTypeName());
            List<DeviceVo> voList = new ArrayList<>();
            for (DeviceVo dv : deviceVoList){
                if(t.getDeviceTypeId().equals(dv.getDeviceTypeId())){
                    String alias = StringUtils.isEmpty(dv.getAlias())?"":dv.getAlias()+"号";
                    String typeName = StringUtils.isEmpty(dv.getAlias())?dv.getDeviceTypeName():"-"+dv.getDeviceTypeName();
                    String deviceNo = StringUtils.isEmpty(dv.getDeviceNo())?"":"-"+dv.getDeviceNo();
                    StringBuilder sb = new StringBuilder().append(alias).append(typeName).append(deviceNo);
                    dv.setJoinName(sb.toString());
                    voList.add(dv);
                }
                deviceVo.setDeviceList(voList);
            }
            list.add(deviceVo);
        }
        return ServerResponse.createBySuccess(list);
    }
}
